<template>
  <div>
    <NavBar />
    <div class="main-content">
      <div class="container">
        <h2>我的订单</h2>
        <v-container fluid class="px-4">
          <v-row no-gutters class="grid-container">
            <!-- 代买需求区域 -->
            <div class="box section-title">需求订单</div>
            <div class="box section-title">服务订单</div>
            <v-col  class="grid-item">
              <table>
                <thead>
                  <tr>
                    <th>订单编号</th>
                    <th>买主学生卡号</th>
                    <th>外送位置</th>
                    <th>代主学生卡号</th>
                    <th>用餐类别</th>
                    <th>餐厅</th>
                    <th>餐品</th>
                    <th>购买数量</th>
                    <th>总金额</th>
                    <th>服务小费</th>
                    <th>订单时间</th>
                  </tr>
                </thead>
                <tbody>
                  <tr v-for="item in requestorders" :key="item.OrderID" @click="complaintOrder1(item)">
                    <td>{{ item.OrderID }}</td>
                    <td>{{ item.Card1 }}(我)</td>
                    <td>{{ item.DeliveryLoc }}</td>
                    <td>{{ item.Card2 }}</td>
                    <td>{{ item.MealType }}</td>
                    <td>{{ item.Restaurant }}</td>
                    <td>{{ item.Dish }}</td>
                    <td>{{ item.Quantity }}</td>
                    <td>{{ item.TotalAmount }}</td>
                    <td>{{ item.ServiceCharge }}</td>
                    <td>{{ item.OrderTime }}</td>
                    <!-- 添加其他数据列 -->
                  </tr>
                </tbody>
              </table>
            </v-col>
            <!-- 代买服务区域 -->
            <v-col  class="grid-item">
              <table>
                <thead>
                  <tr>
                    <th>订单编号</th>
                    <th>买主学生卡号</th>
                    <th>外送位置</th>
                    <th>代主学生卡号</th>
                    <th>用餐类别</th>
                    <th>餐厅</th>
                    <th>餐品</th>
                    <th>购买数量</th>
                    <th>总金额</th>
                    <th>服务小费</th>
                    <th>订单时间</th>
                  </tr>
                </thead>
                <tbody>
                  <tr v-for="item in serviceorders" :key="item.OrderID" @click="complaintOrder2(item)">
                    <td>{{ item.OrderID }}</td>
                    <td>{{ item.Card1 }}</td>
                    <td>{{ item.DeliveryLoc }}</td>
                    <td>{{ item.Card2 }}(我)</td>
                    <td>{{ item.MealType }}</td>
                    <td>{{ item.Restaurant }}</td>
                    <td>{{ item.Dish }}</td>
                    <td>{{ item.Quantity }}</td>
                    <td>{{ item.TotalAmount }}</td>
                    <td>{{ item.ServiceCharge }}</td>
                    <td>{{ item.OrderTime }}</td>
                    <!-- 添加其他数据列 -->
                  </tr>
                </tbody>
              </table>
            </v-col>
          </v-row>
        </v-container>
      </div>
    </div>
  </div>
</template>

<script setup>
import NavBar from './NavBar.vue'
import { ref, onMounted } from 'vue'
import axios from 'axios'
import { useRouter } from 'vue-router';
const router = useRouter()

const requestorders = ref([])
const serviceorders = ref([])


onMounted(() => {
  console.log('mounted');
  getMealOrders();
});

const getMealOrders = async () => {
  const response1 = axios.get('http://localhost:8080/getRequestOrders',{params: {userID: localStorage.getItem('userID')}}) // 将userID作为查询参数传递
  response1.then(res => {
    requestorders.value = res.data
  })
  const response2 = axios.get('http://localhost:8080/getServiceOrders',{params: {userID: localStorage.getItem('userID')}})
  response2.then(res => { 
    serviceorders.value = res.data
  })
}

const complaintOrder1 = (item) => {
  if(confirm('是否举报该订单？')){
    router.push({name: 'OrderComplaint', params: {orderID: item.OrderID, complained: item.Card2}})
  }
}

const complaintOrder2 = (item) => {
  if(confirm('是否举报该订单？')){
    router.push({name: 'OrderComplaint', params: {orderID: item.OrderID, complained: item.Card1}})
  }
}
</script>

<style scoped>
  .container {
    max-width: 95%;
    margin: 2rem auto;
    overflow: hidden;
    text-align: center;
  }
  
  h1 {
    color: #333;
    margin-bottom: 20px;
  }
  h2 {
    color: #d15f1d;
    margin-bottom: 15px;
  }
  .login-options {
    margin-bottom: 20px;
  }

  .btn {
    display: inline-block;
    margin-top: 10px;
    padding: 10px 20px;
    border: none;
    border-radius: 4px;
    background-color: #5cb85c; /* 绿色按钮 */
    color: white;
    font-size: 16px;
    cursor: pointer;
    text-decoration: none;
    margin-right: 10px;
  }

  .btn:hover {
    background-color: #4cae4c; /* 鼠标悬停时的按钮颜色 */
  }

  /* 为容器添加一些内边距 */
  .px-4 {
    padding-right: 16px;
    padding-left: 16px;
  }
  
  /* 移除列之间的间隙 */
  .v-row.no-gutters {
    margin-right: 0;
    margin-left: 0;
  }
  
  /* 网格容器样式 */
  .grid-container {
    display: grid;
    grid-template-columns: 1fr;
    gap: 2rem;
    margin-top: 2rem;
  }
  
  /* 网格项样式 */
  .grid-item {
    background-color: #ffffff;
    border-radius: 12px;
    padding: 1.5rem;
    box-shadow: var(--shadow);
    transition: var(--transition);
    overflow: auto;
    /* 添加相对定位，作为sticky表头的参考容器 */
    position: relative;
  }
  
  .grid-item:hover {
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  }
  
  /* 框样式 */
  .box {
    font-size: 1.5rem; /* 字体大小 */
    font-weight: bold; /* 字体加粗 */
    color: #333; /* 字体颜色 */
    margin-bottom: 16px; /* 与列表间距 */
    position: relative;
  }
  
  /* 列表样式 */
  .request-list, .service-list {
    list-style-type: none; /* 移除列表项前的圆点 */
    padding: 0;
    width: 100%; /* 宽度 */
  }
  
  /* 列表项样式 */
  .request-list li, .service-list li {
    font-size: 1rem; /* 字体大小 */
    line-height: 1.6; /* 行高 */
    color: #555; /* 字体颜色 */
    margin-bottom: 8px;
  }

  td{
    padding: 10px;
    border: 1px solid #ddd;
    text-align: center;
  }
  
  /* 表格容器 */
  .table-container {
    position: relative; /* 添加相对定位 */
    overflow-x: auto;
    margin: 0 -1rem;
    padding: 0 1rem;
    padding-top: 40px; /* 减小顶部内边距 */
  }
  
  /* 表格样式优化 */
  table {
    width: 100%;
    min-width: 1000px; /* 确保表格不会过度压缩 */
    margin: 0;
  }
  
  th {
    position: sticky;
    top: 0;
    background: linear-gradient(to bottom, #ffffff 0%, #f8f9fa 100%);
    padding: 12px 8px;
    text-align: center;
    font-weight: 600;
    border-bottom: 2px solid var(--border-color);
    white-space: nowrap;
    z-index: 2;
  }
  
  td {
    white-space: nowrap; /* 防止内容换行 */
    max-width: 200px; /* 限制单元格最大宽度 */
    overflow: hidden;
    text-overflow: ellipsis; /* 超出显示省略号 */
  }
  
  /* 表格行交替颜色 */
  tr:nth-child(even) {
    background-color: var(--background-color);
  }
  
  tr:hover {
    background-color: rgba(66, 185, 131, 0.05);
  }
  
  /* 自定义滚动条 */
  .grid-item::-webkit-scrollbar {
    width: 8px;
    height: 8px;
  }
  
  .grid-item::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 4px;
  }
  
  .grid-item::-webkit-scrollbar-thumb {
    background: #c1c1c1;
    border-radius: 4px;
  }
  
  .grid-item::-webkit-scrollbar-thumb:hover {
    background: #a8a8a8;
  }
  
  /* 响应式布局优化 */
  @media (min-width: 1400px) {
    .grid-container {
      grid-template-columns: repeat(2, 1fr);
    }
    
    .container {
      max-width: 1800px;
    }
  }
  
  @media (max-width: 768px) {
    .container {
      margin: 1rem;
      padding: 1rem;
    }

    .grid-item {
      padding: 1rem;
    }

    .section-title {
      font-size: 1.2rem;
      position: relative;
    }

    td, th {
      padding: 0.8rem;
      font-size: 0.9rem;
    }
  }
  
  /* 添加淡入动画 */
  .grid-item {
    animation: fadeIn 0.5s ease-out;
  }
  
  @keyframes fadeIn {
    from {
      opacity: 0;
      transform: translateY(20px);
    }
    to {
      opacity: 1;
      transform: translateY(0);
    }
  }

  /* 为每个th添加具体宽度 */
  th:nth-child(1), th:nth-child(2), th:nth-child(3),
  th:nth-child(4), th:nth-child(5), th:nth-child(6),
  th:nth-child(7), th:nth-child(8), th:nth-child(9),
  th:nth-child(10), th:nth-child(11) {
    position: sticky;
    left: auto;
    width: auto;
  }

  /* 表格单元格宽度 */
  th:nth-child(1), td:nth-child(1) { min-width: 100px; } /* 订单编号 */
  th:nth-child(2), td:nth-child(2) { min-width: 120px; } /* 买主学生卡号 */
  th:nth-child(3), td:nth-child(3) { min-width: 120px; } /* 外送位置 */
  th:nth-child(4), td:nth-child(4) { min-width: 120px; } /* 代主学生卡号 */
  th:nth-child(5), td:nth-child(5) { min-width: 100px; } /* 用餐类别 */
  th:nth-child(6), td:nth-child(6) { min-width: 100px; } /* 餐厅 */
  th:nth-child(7), td:nth-child(7) { min-width: 100px; } /* 餐品 */
  th:nth-child(8), td:nth-child(8) { min-width: 100px; } /* 购买数量 */
  th:nth-child(9), td:nth-child(9) { min-width: 100px; } /* 总金额 */
  th:nth-child(10), td:nth-child(10) { min-width: 100px; } /* 服务小费 */
  th:nth-child(11), td:nth-child(11) { min-width: 160px; } /* 订单时间 */

  /* 表格容器阴影效果 */
  .table-container::after {
    content: '';
    position: sticky;
    left: 0;
    right: 0;
    top: 40px; /* 与表头底部对齐 */
    height: 4px;
    background: linear-gradient(to bottom, rgba(0,0,0,0.05), transparent);
    pointer-events: none;
    z-index: 1;
  }

  /* 确保表格内容正确显示 */
  tbody {
    position: relative;
    z-index: 1;
  }

  /* 响应式调整 */
  @media (max-width: 768px) {
    .table-container {
      padding-top: 35px;
    }

    .section-title {
      font-size: 1.2rem;
      margin-bottom: 1rem;
      padding-bottom: 0.6rem;
    }
  }

  /* 修改表头样式 */
  .section-title {
    font-size: 1.4rem;
    font-weight: 600;
    color: #2c3e50;
    margin-bottom: 1.5rem;
    padding-bottom: 0.8rem;
    border-bottom: 2px solid #e9ecef;
    background: #ffffff;
    position: relative;
    z-index: 3;
  }

  /* 表格容器样式 */
  .table-container {
    position: relative;
    overflow-x: auto;
    margin-top: 1rem;
  }

  /* 表头样式 */
  thead {
    position: sticky;
    top: 0;
    z-index: 2;
    background: #ffffff;
  }

  th {
    position: sticky;
    top: 0;
    background: linear-gradient(to bottom, #42b983, #3aa876);
    color: white;
    padding: 12px 8px;
    text-align: center;
    font-weight: 600;
    white-space: nowrap;
    z-index: 2;
  }

  /* 表格行样式 */
  tr:hover {
    background-color: rgba(66, 185, 131, 0.05);
    transition: background-color 0.3s ease;
  }

  /* 单元格样式 */
  td {
    padding: 12px 8px;
    text-align: center;
    vertical-align: middle;
    border-bottom: 1px solid #e9ecef;
  }

  /* 确保表格内容正确显示 */
  tbody {
    position: relative;
    z-index: 1;
  }
</style>